package com.zzty.taskapp.daoImpl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zzty.taskapp.common.DBUtil;
import com.zzty.taskapp.dao.ClassGroupDAO;
import com.zzty.taskapp.entity.ClassGroup;

public class ClassGroupDAOImpls implements ClassGroupDAO {

	public List<ClassGroup> getGroupByClass(int classId) {
		List<ClassGroup> groups=new ArrayList<ClassGroup>();
		try {
		String sql="select * from classgroup where class_id=?";
		PreparedStatement ps = DBUtil.getPreparedStatement(sql);
		ps.setInt(1, classId);
		ResultSet rs;
			rs = ps.executeQuery();
			while(rs.next()){
				ClassGroup classGroup=new ClassGroup();
				classGroup.setClazzId(classId);
				classGroup.setId(rs.getInt("id"));
				classGroup.setStuIds(rs.getString("stu_ids"));
				groups.add(classGroup);
		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return groups;
	}

	public void updateClassGroup(int id,String stu_ids){
		try {
		String sql = "update classgroup set stu_ids = ? where id = ?";
		PreparedStatement pst = DBUtil.getPreparedStatement(sql);
			pst.setString(1, stu_ids);
			pst.setInt(2, id);
			pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void addGroup(int classId,String stu_ids){
		String sql = "insert into classgroup (class_id,stu_ids) values (?,?)";
		PreparedStatement pst = DBUtil.getPreparedStatement(sql);
		try {
			pst.setInt(1, classId);
			pst.setString(2, stu_ids);
			pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	
	public ArrayList<Integer> getIdByClassId(int classid){
		ArrayList<Integer> ids = new ArrayList<Integer>();
		try {
		String sql = "select id from classgroup where class_id = ?";
		PreparedStatement pst = DBUtil.getPreparedStatement(sql);
			pst.setInt(1, classid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				ids.add(rs.getInt("id"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ids;
	}
}
